
#include "include.h"


void recv_screen_package_callback( uint8_t *data, uint16_t size )
{
  if( size > Screen_Recv_Buf_Max )
  {
    return;
  }
  rk.screen.recv_len = size;
  memcpy(rk.screen.recv_buf, data, size);
  rk.flag.recv_screen = true;
}

/*
1.source 待打包的数据缓冲区地址
2.target 打包后数据存储的地址
3.item   打包的数据类型
4.size   待打包的数据个数
5.status 状态*/
uint16_t screen_data_protocol(uint16_t *source, uint8_t *target, int item, uint16_t size, int status)
{
  int i = 0, j = DATA_START;  //i:遍历source数组。j遍历target数组
  int num = 0; //保存source有多少项
  uint16_t start_position = 0;

   //判断当前协议构建是实时数据还是参数配置
  if(item == REAL_TIME_DATA_ITEM) //实时数据
  {
    num = size;
    start_position = REAL_DATA_START_POSITION;
  }
  else if( item == Hyper_Parameter_Item )
  {
    num = size;
    start_position = Addr_Hyper_Parameter_Data;
  }
  else if(item == PARAMETER_CONF_ITEM) //阈值等信息
  {
    num = size;
    start_position = PARAMETER_START_POSITION;
  }
  else if(item == CONT_DATA_ITEM)
  {
    num = size;
    start_position = VERTICAL_MOTION_ADDRESS;
  }
  else if(item == SERVER_IP_ITEM) //server ip数据
  {
    start_position = SERVER_IP_START_POSITION;
  }
  else if(item == COORDINATE) //闸门坐标数据
  {
    start_position = COORDINATE_START_POSITION;
  }
  else if(item == LOGIN_ITEM)
  {
    start_position = LOGIN_DATA_START_POSITION;
  }
  else if(item == COLOR_ITEM)
  {
    start_position = COLOR_DATA_STAR_POSITION;
  }

  //构建协议头部通用部分
  screen_data_protocol_common_part(target, start_position);

  //构建需要上传的数据
  if(item == SERVER_IP_ITEM)
  {
     return 3 + server_ip_protocol(source, target); //3指的是A5 5A 0F这三个字节长度
  }
  else if(item == COORDINATE)//闸门坐标数据
  {
    return waterlock_axis_protocol(source, target);
  }
  else if(item == LOGIN_ITEM)
  {
    return login_protocol(target, status);
  }
  else if(item == COLOR_ITEM)
  {
    return color_protocol(target, status);
  }
  for(i = 0; i < num; j=j+2, i++)
  {
    data_into_arry(target, j, source[i]);
  }
  //添加指令长度
  target[2] = j - 3; //第一个2是for循环结束后j会自动加2，这里需要减去，第二个3是target的前三个下标
  return (j); //返回长度
}
uint16_t color_protocol(uint8_t *target, int status)
{
  int i = DATA_START;
  if(status == LOGIN_OK)
  {
    target[i++] = 0x03;
    target[i++] = 0xC0;
  }
  else if(status == LOGIN_ERROR)
  {
    target[i++] = 0xF8;
    target[i++] = 0x00;
  }
  target[2] = i - 3;
  return i;
}
uint16_t login_protocol(uint8_t *target, int status)
{
  int i = DATA_START;
  if(status == LOGIN_OK)
  {
    target[i++] = 0xD2;
    target[i++] = 0xD1;
  }
  else if(status == LOGIN_ERROR)
  {
    target[i++] = 0xCE;
    target[i++] = 0xB4;
  }
  target[i++] = 0xCA;
  target[i++] = 0xDA;
  target[i++] = 0xC8;
  target[i++] = 0xA8;
  target[i++] = 0xFF;
  target[i++] = 0xFF;
  target[2] = i - 3;
  return i;
}
uint16_t waterlock_axis_protocol(uint16_t *source, uint8_t *target)
{
  int i = DATA_START;
  target[i++] = 0x28;
  target[i++] = 0x07;
  target[i++] = 0x00;
  target[i++] = 0x01;
  target[i++] = (source[0] >> 8) & 0xFF;
  target[i++] = (source[0] >> 0) & 0xFF;
  target[i++] = (source[1] >> 8) & 0xFF;
  target[i++] = (source[1] >> 0) & 0xFF;
  target[i++] = 0x00;
  target[i++] = 0x05;
  target[2] = i - 3;
  return (i);
}
uint16_t server_ip_protocol(uint16_t *source, uint8_t *target)
{
  int len = 3; //记录82 00 20这三个字节的长度，后面是serverip的数据长度
  uint8_t *temp = target;
  temp += 6;
  for (int i = 0; i < SERVER_IP_ITEM; i++)
  {
    if (i > 0) {
      // 在每个字节之间插入点（`.`）
      *temp++ = '.';
      len++;
    }
  // 使用snprintf将每个字节转换为字符串，并存储到target中
        int written = snprintf((char *)temp, 4, "%u", source[i]); // 最多写入size-1个字符
        // 计算写入的字符数，移动temp指针
        len += written;
        temp += written;
  }
  target[2] = len;
  return len;
}

void screen_data_protocol_common_part(uint8_t *target, uint16_t start_position)
{
  target[0] = 0x5A;
  target[1] = 0xA5;
  target[2] = 0; //记录指令长度：从指令开始到整包结束
  target[3] = 0x82; //向指定变量存储器地址写数据
  target[4] = (start_position >> 8) & 0xFF ;
  target[5] = (start_position >> 0) & 0xFF; //起始地址
}

void data_into_arry(uint8_t *target, int index, uint16_t data)
{
  target[index + 0] = (data >> 8) & 0xFF;
  target[index + 1] = (data >> 0) & 0xFF;
}
void clear_screen_password( void )
{
  rk.screen.password = 0;
  rk.screen.send_buf[0] = 0x5A;
  rk.screen.send_buf[1] = 0xA5;
  rk.screen.send_buf[2] = 0x05;
  rk.screen.send_buf[3] = 0x82;
  rk.screen.send_buf[4] = 0x20;
  rk.screen.send_buf[5] = 0x20;
  rk.screen.send_buf[6] = 0x00;
  rk.screen.send_buf[7] = 0x00;
  rk.screen.send_len = 8;
  HAL_UART_Transmit(&huart2, rk.screen.send_buf, rk.screen.send_len, 0xFF );
}
void switch_Page( uint8_t Page )
{
  rk.screen.send_buf[0] = 0x5A;
  rk.screen.send_buf[1] = 0xA5;
  rk.screen.send_buf[2] = 0x04;
  rk.screen.send_buf[3] = 0x80;
  rk.screen.send_buf[4] = 0x03;
  rk.screen.send_buf[5] = 0x00;

  rk.screen.send_buf[6] = Page;
  rk.screen.interface_option = Page;

  if( Page == Error_Page )
  {
    rk.screen.send_buf[6] = 0x08;
  }
  rk.screen.send_len = 7;
  HAL_UART_Transmit(&huart2, rk.screen.send_buf, rk.screen.send_len, 0xFF );
}
// 设置超参数界面
void set_hyper_parameter_page( void )
{
  if( rk.hyper_parameter.addr.region  == Shaanxi_Code )
  {
    switch_Page( Hpr_Prm_Page );
  }
  memset( rk.screen.source_buf, NULL, Screen_Source_Buf_Max );
  rk.screen.source_buf[0]   = rk.hyper_parameter.addr.stake_front;                                              // 桩号（前）
  rk.screen.source_buf[1]   = rk.hyper_parameter.addr.stake_back;                                               // 桩号（后）
  rk.screen.source_buf[2]   = rk.hyper_parameter.addr.serial;                                                   // 顺序编号
  rk.screen.source_buf[3]   = rk.hyper_parameter.password;                                                      // 设备密钥//
  rk.screen.source_buf[4]   = (uint16_t)(rk.hyper_parameter.hardware_version * 10);                             // 硬件版本
  rk.screen.source_buf[5]   = (uint16_t)(rk.hyper_parameter.software_version * 10);                             // 软件版本
  rk.screen.source_buf[6]   = rk.hyper_parameter.encoder_upper_limit >> 16;                                     // 编码器上限
  rk.screen.source_buf[7]   = rk.hyper_parameter.encoder_upper_limit;                                           // 编码器上限
  rk.screen.source_buf[8]   = rk.hyper_parameter.encoder_lower_limit >> 16;                                     // 编码器下限
  rk.screen.source_buf[9]   = rk.hyper_parameter.encoder_lower_limit;                                           // 编码器下限
  rk.screen.source_buf[10]  = rk.hyper_parameter.gate_front_water_stage_upper_limit;                            // 闸前水位上限
  rk.screen.source_buf[11]  = rk.hyper_parameter.gate_front_water_stage_lower_limit;                            // 闸前水位下限
  rk.screen.source_buf[12]  = rk.hyper_parameter.gate_back_water_stage_upper_limit;                             // 闸后水位上限
  rk.screen.source_buf[13]  = rk.hyper_parameter.gate_back_water_stage_lower_limit;                             // 闸后水位下限
  rk.screen.source_buf[14]  = (short)(rk.hyper_parameter.voltage_upper_limit * 0.01f);                            // 电压上限
  rk.screen.source_buf[15]  = (short)(rk.hyper_parameter.voltage_lower_limit * 0.01f);                            // 电压下限
  rk.screen.source_buf[16]  = (short)(rk.hyper_parameter.current_upper_limit * 0.01f);                            // 电流上限
  rk.screen.source_buf[17]  = (short)(rk.hyper_parameter.current_lower_limit * 0.01f);                            // 电流下限
  rk.screen.source_buf[18]  = rk.hyper_parameter.temperature_upper_limit;                                       // 温度上限
  rk.screen.source_buf[19]  = rk.hyper_parameter.temperature_lower_limit;                                       // 温度下限
  rk.screen.source_buf[20]  = rk.hyper_parameter.gate_front_water_stage_addr;                                   // 闸前水位地址
  rk.screen.source_buf[21]  = rk.hyper_parameter.gate_back_water_stage_addr;                                    // 闸后水位地址
  rk.screen.source_buf[22]  = rk.hyper_parameter.gate_height >> 16;                                             // 闸门高度
  rk.screen.source_buf[23]  = rk.hyper_parameter.gate_height;                                                   // 闸门高度
  rk.screen.source_buf[24]  = rk.hyper_parameter.gate_width >> 16;                                              // 闸门宽度
  rk.screen.source_buf[25]  = rk.hyper_parameter.gate_width;                                                    // 闸门宽度
  rk.screen.source_buf[26]  = rk.hyper_parameter.gate_full_open_time >> 16;                                     // 闸门全开时间
  rk.screen.source_buf[27]  = rk.hyper_parameter.gate_full_open_time;                                           // 闸门全开时间
  rk.screen.source_buf[28]  = (uint16_t)(rk.hyper_parameter.angle_to_displacement_coefficient * 1000) >> 16;    // 角度转换为位移的系数
  rk.screen.source_buf[29]  = (uint16_t)(rk.hyper_parameter.angle_to_displacement_coefficient * 1000);          // 角度转换为位移的系数
  rk.screen.source_buf[30]  = rk.hyper_parameter.upload_target_server_ip[0];                                    // 服务器ip地址
  rk.screen.source_buf[31]  = rk.hyper_parameter.upload_target_server_ip[1];                                    // 服务器ip地址
  rk.screen.source_buf[32]  = rk.hyper_parameter.upload_target_server_ip[2];                                    // 服务器ip地址
  rk.screen.source_buf[33]  = rk.hyper_parameter.upload_target_server_ip[3];                                    // 服务器ip地址
  rk.screen.source_buf[34]  = rk.hyper_parameter.upload_target_port;                                            // 服务器端口
  rk.screen.source_buf[35]  = rk.hyper_parameter.reconnect_server_timer;                                        // 重连时间 :单位 ： 120
  screen_send_data( rk.screen.source_buf, Hyper_Parameter_Item, Screen_Send_Hyr_Prm_Data_Len );
}

// 获取超参数界面
void get_hyper_parameter_page( void )
{
  rk.screen.send_buf[0] = 0x5A;
  rk.screen.send_buf[1] = 0xA5;
  rk.screen.send_buf[2] = 0x04;
  rk.screen.send_buf[3] = 0x83;
  rk.screen.send_buf[4] = 0x10;
  rk.screen.send_buf[5] = 0x00;
  rk.screen.send_buf[6] = Screen_Send_Hyr_Prm_Data_Len;
  rk.screen.send_len = 7;
  HAL_UART_Transmit(&huart2, rk.screen.send_buf, rk.screen.send_len, 0xFF );
  rk.flag.screen_set_hpr_prm = false;
}
void response_operation(uint16_t address, uint16_t value)
{
   char str[128];
  switch(address)
  {
    // 密码接受
    case Addr_Password:
      rk.screen.password = value;
      break;
    // 登录
    case Addr_Login:

      if( rk.screen.password == Screen_Password )
      {
        rk.flag.screen_login = true;
        switch_Page( Monitoring_Page );

        clear_screen_password();
      }
      else
      {
        switch_Page( Error_Page );
      }

      break;
    //

    //  切换界面
    case Addr_Auto_Page:
      switch_Page( value );
      break;

    /* 上升 */
    case Addr_Set_Gate_Up:
      rk.flag.gate_control_change = true;
      set_device_mode( Local_Control_Mode );
      set_control_mode( Manual_Control_Mode );
      sprintf(str, "recv scrren control motor up, encoder value : %d current : %f\r\n", rk.encoder.turn_nums, rk.ina226.current );
      uart_log( str );
      set_gate_status( UP );
      break;

    /* 下降 */
    case Addr_Set_Gate_Down:
      rk.flag.gate_control_change = true;
      set_device_mode( Local_Control_Mode );
      set_control_mode( Manual_Control_Mode );
      sprintf(str, "recv scrren control motor down, encoder value : %d current : %f\r\n", rk.encoder.turn_nums, rk.ina226.current );
      uart_log( str );
      set_gate_status( DOWN );
      break;

    /* 停止 */
    case Addr_Set_Gate_Stop:
      set_device_mode( Local_Control_Mode );
      set_control_mode( Manual_Control_Mode );

      sprintf(str, "recv scrren control motor stop, encoder value : %d current : %f\r\n", rk.encoder.turn_nums, rk.ina226.current );
      uart_log( str );
      set_gate_status( STOP );
      break;

    /* 自动控制开关 */
    case Addr_Auto_Control:
      set_control_mode( Auto_Control_Mode );
      break;

      /* 设置自动控制方式 */
    case TRAFFIC_CONTROL_SET_ADDRESS:
      set_control_mode( Auto_Control_Mode );
      set_gate_auto_cmdcode(value);
      break;

    /* 恒定流量控制 */
    case Addr_Constant_Flowmeter:
//      set_gate_status( STOP );
//      set_gate_auto_value( Command_Constant_Flowmeter, value );
      break;

    /* 恒定闸门开度控制 */
    case Addr_Constant_Gate_Opening:
      set_gate_auto_value( Command_Constant_Gate_Opening_Degree, value );
      break;

    /* 恒定水位控制 */
    case Addr_Constant_Water_Level:
//      set_gate_status( STOP );
//      set_gate_auto_value( Command_Constant_Water_Level, value );
      break;

    /* 下发超参数 */
    case Addr_Set_Hyper_Paremeter:
      set_hyper_parameter_page();
      break;

    /* 获取超参数（请求数据） */
    case Addr_Get_Hyper_Paremeter:
      rk.flag.screen_set_hpr_prm = true;
      break;

    /* 获取超参数（接收数据） */
    case Addr_Hyper_Parameter_Data:

      break;

    /* 复位 */
    case Addr_Reset:
      rk.flag.system_restart = true;
      break;
    /* 关屏 */
    case Addr_Set_Screen_Close:
      rk.flag.screen_close = true;
      break;
    default:
        break;
  }
}

ErrorStatus screen_data_analysis( SCREEN *screen )
{
  if(screen->recv_buf[0] != 0x5A || screen->recv_buf[1] != 0xA5 || screen->recv_buf[3] != 0x83)
  {
    return ERROR;
  }
  screen->address = (screen->recv_buf[4] << 8) | (screen->recv_buf[5]);
  screen->recv_value = (screen->recv_buf[7] << 8) | (screen->recv_buf[8]);
  return SUCCESS;
}
void screen_set_hyper_parameter(unsigned char *data )
{
  uint16_t datapos = 0;
  int16_t temp;
  float    temp_f;
  rk.hyper_parameter.password                                 = (data[datapos] << 8) | data[datapos+1];               // 设备
  datapos += 2;
  rk.hyper_parameter.password                                 = (data[datapos] << 8) | data[datapos+1];               // 设备密钥
  datapos += 2;
  rk.hyper_parameter.password                                 = (data[datapos] << 8) | data[datapos+1];               // 设备密钥
  datapos += 2;

  rk.hyper_parameter.password                                 = (data[datapos] << 8) | data[datapos+1];               // 设备密钥
  datapos += 2;

  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.hardware_version                         = (float)temp * 0.1f;               // 硬件版本
  datapos += 2;
  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.software_version                         = (float)temp * 0.1f;               // 软件版本
  datapos += 2;

  /* 门限设置 */
  rk.hyper_parameter.encoder_upper_limit                      = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];                 // 编码器上限
  datapos += 4;
  rk.hyper_parameter.encoder_lower_limit                      = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];                 // 编码器下限
  datapos += 4;

  rk.hyper_parameter.gate_front_water_stage_upper_limit       = (data[datapos] << 8) | data[datapos+1];                  // 闸前水位上限
  datapos += 2;
  rk.hyper_parameter.gate_front_water_stage_lower_limit       = (data[datapos] << 8) | data[datapos+1];                  // 闸前水位下限
  datapos += 2;
  rk.hyper_parameter.gate_back_water_stage_upper_limit        = (data[datapos] << 8) | data[datapos+1];                  // 闸后水位上限
  datapos += 2;
  rk.hyper_parameter.gate_back_water_stage_lower_limit        = (data[datapos] << 8) | data[datapos+1];                  // 闸后水位下限
  datapos += 2;

  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.voltage_upper_limit                      = (float)temp * 100;                // 电压上限
  datapos += 2;
  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.voltage_lower_limit                      = (float)temp * 100;                // 电压下限
  datapos += 2;
  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.current_upper_limit                      = (float)temp * 100;                // 电流上限
  datapos += 2;
  temp = (data[datapos] << 8) | data[datapos+1];
  rk.hyper_parameter.current_lower_limit                      = (float)temp * 100;                // 电流下限
  datapos += 2;

  rk.hyper_parameter.temperature_upper_limit                  = (data[datapos] << 8) | data[datapos+1];                   // 温度上限
  datapos += 2;
  rk.hyper_parameter.temperature_lower_limit                  = (data[datapos] << 8) | data[datapos+1];                   // 温度下限
  datapos += 2;

  /* 设备参数地址 */
  rk.hyper_parameter.gate_front_water_stage_addr              = data[datapos+1];                 // 闸前水位地址
  datapos += 2;
  rk.hyper_parameter.gate_back_water_stage_addr               = data[datapos+1];                 // 闸后水位地址
  datapos += 2;

  rk.hyper_parameter.gate_height                              = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];                 // 闸门高度
  datapos += 4;
  rk.hyper_parameter.gate_width                               = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];                 // 闸门宽度
  datapos += 4;
  rk.hyper_parameter.gate_full_open_time                      = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];                 // 闸门全开时间
  datapos += 4;

  temp = (data[datapos] << 24) | (data[datapos+1] << 16) | (data[datapos+2] << 8) | data[datapos+3];
  rk.hyper_parameter.angle_to_displacement_coefficient        = temp * 0.001f;                  // 角度转换为位移的系数
  datapos += 4;

  /* 服务器 */
  rk.hyper_parameter.upload_target_server_ip[0]               = data[datapos+1];                   // 服务器ip地址
  datapos += 2;
  rk.hyper_parameter.upload_target_server_ip[1]               = data[datapos+1];                   // 服务器ip地址
  datapos += 2;
  rk.hyper_parameter.upload_target_server_ip[2]               = data[datapos+1];                   // 服务器ip地址
  datapos += 2;
  rk.hyper_parameter.upload_target_server_ip[3]               = data[datapos+1];                   // 服务器ip地址
  datapos += 2;
  rk.hyper_parameter.upload_target_port                       = (data[datapos] << 8) | data[datapos+1];                  // 服务器端口
  datapos += 2;
  rk.hyper_parameter.reconnect_server_timer                   = (data[datapos] << 8) | data[datapos+1];                  // 重连时间
  datapos += 2;

  initflag_set( Extflash_Init_Flag );
  HAL_Delay(20);
  hyper_parameter_set();
  HAL_Delay(20);
  parameter_set();
  HAL_Delay(20);
}
/* 接收串口屏数据处理函数 */
void recv_screen_package_handle( void )
{
  uint8_t ret;
  uint16_t recv_addr;
  ret = screen_data_analysis( &rk.screen );


  recv_addr = (rk.screen.recv_buf[4] << 8) | rk.screen.recv_buf[5];
  if( recv_addr == Addr_Hyper_Parameter_Data )
  {
    screen_set_hyper_parameter( rk.screen.recv_buf+7 );
  }
  if( ret == SUCCESS  )
  {
    uart_log("recv screen data!\r\n");
    response_operation( rk.screen.address, rk.screen.recv_value );
  }

  rk.flag.recv_screen = false;
}
void screen_send_data( unsigned short *data, unsigned char item, uint16_t size  )
{
  uint16_t len;
  len = screen_data_protocol( data, rk.screen.send_buf, item, size, 0 );
  HAL_UART_Transmit_DMA( &huart2, rk.screen.send_buf, len );
  rk.timer.RS485_transmit_timer = 0;
  while( __HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC) == 0 )
  {
    if( rk.timer.RS485_transmit_timer >= RS485_Transmit_Timeout )
    {
      break;
    }
  }
  __HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_TC);
}

void set_screen_gate_pos( void )
{
  uint8_t len;
  float opening_degree_percent;
  rk.screen.xy_value[Gate_x] = Gate_X_Pos;

  opening_degree_percent = (float)rk.gate.opening_degree / (float)Gate_Opening_Degree_100;

  rk.screen.xy_value[Gate_y] = Gate_Y_Low_Pos - (Gate_Y_Low_Pos - Gate_Y_High_Pos) * opening_degree_percent;
//  rk.screen.xy_value[Gate_y] = 354;
  len = screen_data_protocol( rk.screen.xy_value, rk.screen.send_xy_buf, COORDINATE, Screen_Send_Gate_Opening_Degree_Len, 0 );
  HAL_UART_Transmit_DMA( &huart2, rk.screen.send_xy_buf, len );
  rk.timer.RS485_transmit_timer = 0;
  while( __HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC) == 0 )
  {
    if( rk.timer.RS485_transmit_timer >= RS485_Transmit_Timeout )
    {
      break;
    }
  }
  __HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_TC);
}

void screen_init_success( void )
{
  switch_Page( Login_Page );
}

void switch_screen_open( void )
{
  static uint8_t flag_first_push = 1;
  static uint8_t flag_first_send = 1;
  if(RK_PROJECT == XY_PROJECT)
  {
    // 开
    if( !GPIO_DI0_MCU_ReadPin && flag_first_push )
    {
      flag_first_push = 0;
      GPIO_Display_EN_RESET;
      HAL_Delay(1000);
      GPIO_MCU_WDI_Toggle;
      HAL_Delay(1000);
      GPIO_MCU_WDI_Toggle;
      HAL_Delay(1000);
      GPIO_MCU_WDI_Toggle;
      HAL_Delay(1000);
      screen_init_success();

      rk.parameter.control_object = Local_Control_Mode;
      rk.flag.control_mode_change = true;

    }
    else if( GPIO_DI0_MCU_ReadPin && !flag_first_push )
    {
      flag_first_push = 1;
      GPIO_Display_EN_SET;

      rk.parameter.control_object = Remote_Control_Mode;
      rk.flag.control_mode_change = true;
      rk.flag.screen_login = false;
    }
  }
  else
  {
    if( !GPIO_DI3_MCU_ReadPin )
    {
      HAL_Delay(10);
      if( !GPIO_DI3_MCU_ReadPin )
      {
        while( !GPIO_DI3_MCU_ReadPin )
        {
          HAL_Delay(100);
          GPIO_MCU_WDI_Toggle;
        }

        uart_log( "screen open!\r\n" );
        GPIO_Display_EN_RESET;
        HAL_Delay(1000);
        GPIO_MCU_WDI_Toggle;
        HAL_Delay(1000);
        GPIO_MCU_WDI_Toggle;
        HAL_Delay(1000);
        GPIO_MCU_WDI_Toggle;
        HAL_Delay(1000);
        screen_init_success();
        rk.parameter.control_object = Local_Control_Mode;
        rk.flag.control_mode_change = true;
        rk.timer.screen_stop_push_timer = 0;
      }
    }


    if ( (rk.flag.screen_close == true) || (rk.timer.screen_stop_push_timer >= Screen_Stop_Push_Max) && (rk.parameter.control_object == Local_Control_Mode))
    {
      rk.flag.screen_close = false;
      rk.parameter.control_object = Remote_Control_Mode;
      rk.flag.control_mode_change = true;
      rk.flag.screen_login = false;
      GPIO_Display_EN_SET;
    }
  }
}
/* 刷新串口屏数据 */
void refresh_screen_data( void )
{
  uint8_t bit0_fan  = 0;
  uint8_t bit1_min  = 0;
  uint8_t bit2_down = 0;
  uint8_t bit3_up   = 0;
  uint8_t bit4_max  = 0;

  uint8_t ret = 0;

  switch_screen_open();

  if( rk.parameter.control_object == Remote_Control_Mode )
  {
    return ;
  }
  /* 接受串口屏数据 */
  if(rk.flag.recv_screen == 1)
  {
    recv_screen_package_handle();
    rk.timer.screen_stop_push_timer = 0;
  }

  /* 没注册成功 -> 返回 */
  if( rk.flag.screen_login == false )
  {
    return;
  }
  /* 获取超参数 */
  if( rk.flag.screen_set_hpr_prm == true )
  {
    get_hyper_parameter_page();
  }

  /* 定时刷新串口屏数据 */
  if( rk.timer.screen_refresh_timer >= Screen_Refresh_Timer_Max )
  {
    switch( rk.screen.interface_option )
    {
/**
第一个界面上传的数据：1.实时数据 2.闸门位置 3.自动控制数据
**/
      case Auto_Page :

      case Monitoring_Page  :
        memset( rk.screen.source_buf, NULL, Screen_Source_Buf_Max );
        rk.screen.source_buf[0]  = (uint16_t)(rk.ina226.voltage * 0.01f);                       // 电池电压
        rk.screen.source_buf[1]  = (uint16_t)(rk.ina226.current );                              // 电池电流
        rk.screen.source_buf[2]  = (uint16_t)rk.flowmeter.gata_front_water_stage;               // 闸前水位，单位：mm
        rk.screen.source_buf[3]  = (uint16_t)rk.flowmeter.gata_back_water_stage;                // 闸后水位，单位：mm
        rk.screen.source_buf[4]  = (uint16_t)(rk.flowmeter.instant_water_discharge * 100.0f);   // 瞬时流量，单位m³/s
        rk.screen.source_buf[5]  = (rk.flowmeter.acc_water_discharge ) >> 16;                   // 累计流量，单位m³
        rk.screen.source_buf[6]  = rk.flowmeter.acc_water_discharge * 10;
        rk.screen.source_buf[7]  = Gate_Opening_Degree_100;                                     // 开度，单位：mm
        rk.screen.source_buf[8]  = (uint16_t)(rk.ds18b20.temperature * 10.0f);                  // 温度：℃
        rk.screen.source_buf[9]  = 30;                                                          // 信号强度
        rk.screen.source_buf[10] = rk.flag.device_login_success;                                // 通讯状态
        rk.screen.source_buf[11] = rk.gate.opening_degree;                                      // 闸门最大开度

        if(rk.gate.move_limit_pos == Gate_Move_To_Up_Limit )
        {
          bit4_max = 1;
        }
        if( rk.gate.detection_move_state == Gate_State_Is_Up )
        {
          bit3_up  = 1;
        }
        if( rk.gate.detection_move_state == Gate_State_Is_Down )
        {
          bit2_down = 1;
        }
        if(rk.gate.move_limit_pos == Gate_Move_To_Down_Limit )
        {
          bit1_min = 1;
        }
        if( GPIO_FAN_ReadPin )
        {
          bit0_fan = 1;
        }
        rk.screen.source_buf[12] =  ((bit4_max  << 4) & 0x10)
                                  | ((bit3_up   << 3) & 0x08)
                                  | ((bit2_down << 2) & 0x04)
                                  | ((bit1_min  << 1) & 0x02)
                                  | ((bit0_fan      ) & 0x01) & 0x1F;


        rk.screen.source_buf[13] = rk.upload.alarm_status;


        screen_send_data( rk.screen.source_buf, REAL_TIME_DATA_ITEM, Screen_Send_Data_Len );

        set_screen_gate_pos();

        break;
/**
第二个界面上传的数据 暂无
**/
//      case Set_Page:
//
//        break;

      default :
        break;
    }
    rk.timer.screen_refresh_timer = 0;
  }
}